﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChurchDiary.DAL.DAL;
using ChurchDiary.DAL.Contract;
using System.Data.Common;
using System.Data;
using ChurchDiary.TableEntity;
using ChurchDiary.Common;

namespace ChurchDiary.DAL.Methods
{
    public class DAL_Menu : DataAccess, IDALMenu
    {
        public EntityMenuList GetAllMenus(bool IsSuperUser, Guid Userid)
        {
            EntityMenuList entitymenu = new EntityMenuList();
            try
            {
                List<EntityMainMenu> lstMainMenu = new List<EntityMainMenu>();
                List<EntityMenuCategory> lstMenuCategory = new List<EntityMenuCategory>();
                List<EntitySubMenu> lstSubMenu = new List<EntitySubMenu>();
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_MenuCategory]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@IsSuperUser", DbType.Boolean, IsSuperUser);
                    SQLServer.AddInParameter(dbcommand, "@userId", DbType.Guid, Userid);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        using (IDataReader OReader = SQLServer.ExecuteReader(dbcommand))
                        {
                            if (null == OReader)
                            {
                                return entitymenu;
                            }
                            while (OReader.Read())
                            {
                                EntityMainMenu ObjMainMenu = new EntityMainMenu();
                                ObjMainMenu.MainMenuID = (int)OReader["MainMenuID"];
                                ObjMainMenu.MenuCategoryid = (int)OReader["MenuCategoryId"];
                                ObjMainMenu.MenuName = OReader["MenuName"].ToString();
                                ObjMainMenu.MenuIcon = OReader["MenuIcon"].ToString();
                                ObjMainMenu.MenuTitle = OReader["MenuTitle"].ToString();
                                ObjMainMenu.Controllername = OReader["Controllername"].ToString();
                                ObjMainMenu.ActionName = OReader["ActionName"].ToString();
                                ObjMainMenu.Url = OReader["Url"].ToString();
                                ObjMainMenu.OrderNo = (int)OReader["OrderNo"];
                                ObjMainMenu.PageName = OReader["PageName"].ToString();
                                ObjMainMenu.PageDesc = OReader["PageDesc"].ToString();
                                ObjMainMenu.IsActive = (bool)OReader["IsActive"];
                                lstMainMenu.Add(ObjMainMenu);
                            }
                            entitymenu.entitymainmenu = lstMainMenu;
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                EntityMenuCategory ObjMenu = new EntityMenuCategory();
                                ObjMenu.id = (int)OReader["MenuCategoryId"];
                                ObjMenu.MenuCategoryId = (int)OReader["MenuCategoryId"];
                                ObjMenu.MenuCategoryName = OReader["MenuCategoryName"].ToString();
                                ObjMenu.IsActive = (bool)OReader["IsActive"];
                                lstMenuCategory.Add(ObjMenu);
                            }
                            entitymenu.entitymenucategory = lstMenuCategory;
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                EntitySubMenu ObjMainMenu = new EntitySubMenu();
                                ObjMainMenu.SubMenuID = (int)OReader["SubMenuID"];
                                ObjMainMenu.MainMenuID = (int)OReader["MainMenuID"];
                                ObjMainMenu.SubMenuName = OReader["SubMenuName"].ToString();
                                ObjMainMenu.SubMenuIcon = OReader["SubMenuIcon"].ToString();
                                ObjMainMenu.SubMenuTitle = OReader["SubMenuTitle"].ToString();
                                ObjMainMenu.SubMenuUrl = OReader["SubMenuUrl"].ToString();
                                ObjMainMenu.Controllername = OReader["Controllername"].ToString();
                                ObjMainMenu.ActionName = OReader["ActionName"].ToString();
                                ObjMainMenu.Area = OReader["Area"].ToString();
                                ObjMainMenu.IsActive = (bool)OReader["IsActive"];
                                ObjMainMenu.PageName = OReader["PageName"].ToString();
                                ObjMainMenu.PageDesc = OReader["PageDesc"].ToString();
                                ObjMainMenu.OrderNo = (int)OReader["OrderNo"];
                                lstSubMenu.Add(ObjMainMenu);
                            }
                            entitymenu.entitysubMenu = lstSubMenu;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return entitymenu;
        }
        public List<EntitySubMenu> GetSubMenus()
        {
            List<EntitySubMenu> lstSubMenu = new List<EntitySubMenu>();
            try
            {
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Menus]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@MenuType", DbType.String, "SUBMENU");
                    SQLServer.AddInParameter(dbcommand, "@Mode", DbType.String, "LIST");
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        using (IDataReader OReader = SQLServer.ExecuteReader(dbcommand))
                        {

                            while (OReader.Read())
                            {
                                EntitySubMenu ObjMainMenu = new EntitySubMenu();
                                ObjMainMenu.SubMenuID = (int)OReader["SubMenuID"];
                                ObjMainMenu.MainMenuID = (int)OReader["MainMenuID"];
                                ObjMainMenu.SubMenuName = OReader["SubMenuName"].ToString();
                                ObjMainMenu.SubMenuIcon = OReader["SubMenuIcon"].ToString();
                                ObjMainMenu.SubMenuTitle = OReader["SubMenuTitle"].ToString();
                                ObjMainMenu.SubMenuUrl = OReader["SubMenuUrl"].ToString();
                                ObjMainMenu.Controllername = OReader["Controllername"].ToString();
                                ObjMainMenu.ActionName = OReader["ActionName"].ToString();
                                ObjMainMenu.Area = OReader["Area"].ToString();
                                ObjMainMenu.IsActive = (bool)OReader["IsActive"];
                                ObjMainMenu.PageName = OReader["PageName"].ToString();
                                ObjMainMenu.PageDesc = OReader["PageDesc"].ToString();
                                ObjMainMenu.OrderNo = (int)OReader["OrderNo"];
                                lstSubMenu.Add(ObjMainMenu);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return lstSubMenu;
        }
        public EntitySubMenu GetSubMenu(int MenuId)
        {
            EntitySubMenu ObjMainMenu = new EntitySubMenu();
            try
            {
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Menus]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@MenuType", DbType.String, "SUBMENU");
                    SQLServer.AddInParameter(dbcommand, "@Mode", DbType.String, "ONE");
                    SQLServer.AddInParameter(dbcommand, "@Categoryid", DbType.Int32, MenuId);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        using (IDataReader OReader = SQLServer.ExecuteReader(dbcommand))
                        {

                            while (OReader.Read())
                            {
                                ObjMainMenu.SubMenuID = (int)OReader["SubMenuID"];
                                ObjMainMenu.MainMenuID = (int)OReader["MainMenuID"];
                                ObjMainMenu.SubMenuName = OReader["SubMenuName"].ToString();
                                ObjMainMenu.SubMenuIcon = OReader["SubMenuIcon"].ToString();
                                ObjMainMenu.SubMenuTitle = OReader["SubMenuTitle"].ToString();
                                ObjMainMenu.SubMenuUrl = OReader["SubMenuUrl"].ToString();
                                ObjMainMenu.Controllername = OReader["Controllername"].ToString();
                                ObjMainMenu.ActionName = OReader["ActionName"].ToString();
                                ObjMainMenu.Area = OReader["Area"].ToString();
                                ObjMainMenu.IsActive = (bool)OReader["IsActive"];
                                ObjMainMenu.PageName = OReader["PageName"].ToString();
                                ObjMainMenu.MenuCategoryId = (int)OReader["MenuCategoryId"];
                                ObjMainMenu.PageDesc = OReader["PageDesc"].ToString();
                                ObjMainMenu.OrderNo = (int)OReader["OrderNo"];
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return ObjMainMenu;
        }
        public string InsertSubMenu(EntitySubMenu SubMenu)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_Submenus]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@MainMenuID", DbType.String, SubMenu.MainMenuID);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuName", DbType.String, SubMenu.SubMenuName);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuIcon", DbType.String, SubMenu.SubMenuIcon);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuTitle", DbType.String, SubMenu.SubMenuTitle);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuUrl", DbType.String, SubMenu.SubMenuUrl);
                    SQLServer.AddInParameter(dbCommand, "@Controllername", DbType.String, SubMenu.Controllername);
                    SQLServer.AddInParameter(dbCommand, "@ActionName", DbType.String, SubMenu.ActionName);
                    SQLServer.AddInParameter(dbCommand, "@IsActive", DbType.Boolean, SubMenu.IsActive);
                    SQLServer.AddInParameter(dbCommand, "@OrderNo", DbType.Int32, SubMenu.OrderNo);
                    SQLServer.AddInParameter(dbCommand, "@PageName", DbType.String, SubMenu.PageName);
                    SQLServer.AddInParameter(dbCommand, "@PageDesc", DbType.String, SubMenu.PageDesc);
                    SQLServer.AddInParameter(dbCommand, "@Area", DbType.String, SubMenu.Area);
                    SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.INS);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string UpdateSubMenu(EntitySubMenu SubMenu)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_Submenus]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@SubMenuID", DbType.Int32, SubMenu.SubMenuID);
                    SQLServer.AddInParameter(dbCommand, "@MainMenuID", DbType.String, SubMenu.MainMenuID);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuName", DbType.String, SubMenu.SubMenuName);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuIcon", DbType.String, SubMenu.SubMenuIcon);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuTitle", DbType.String, SubMenu.SubMenuTitle);
                    SQLServer.AddInParameter(dbCommand, "@SubMenuUrl", DbType.String, SubMenu.SubMenuUrl);
                    SQLServer.AddInParameter(dbCommand, "@Controllername", DbType.String, SubMenu.Controllername);
                    SQLServer.AddInParameter(dbCommand, "@ActionName", DbType.String, SubMenu.ActionName);
                    SQLServer.AddInParameter(dbCommand, "@IsActive", DbType.Boolean, SubMenu.IsActive);
                    SQLServer.AddInParameter(dbCommand, "@OrderNo", DbType.Int32, SubMenu.OrderNo);
                    SQLServer.AddInParameter(dbCommand, "@PageName", DbType.String, SubMenu.PageName);
                    SQLServer.AddInParameter(dbCommand, "@PageDesc", DbType.String, SubMenu.PageDesc);
                    SQLServer.AddInParameter(dbCommand, "@Area", DbType.String, SubMenu.Area);
                    SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.UPD);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string DeleteSubMenu(int SubMenuId)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_Submenus]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@SubMenuID", DbType.Int32, SubMenuId);
                    SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.DEL);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public List<EntityMainMenu> GetMainMenus(int CategoryId)
        {
            List<EntityMainMenu> lstMainMenu = new List<EntityMainMenu>();
            try
            {
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Menus]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@MenuType", DbType.String, "MAINMENU");
                    SQLServer.AddInParameter(dbcommand, "@Mode", DbType.String, "SEARCH");
                    SQLServer.AddInParameter(dbcommand, "@Categoryid", DbType.Int32, CategoryId);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        using (IDataReader OReader = SQLServer.ExecuteReader(dbcommand))
                        {

                            while (OReader.Read())
                            {
                                EntityMainMenu ObjMainMenu = new EntityMainMenu();
                                ObjMainMenu.MainMenuID = (int)OReader["MainMenuID"];
                                ObjMainMenu.MenuCategoryid = (int)OReader["MenuCategoryId"];
                                ObjMainMenu.MenuName = OReader["MenuName"].ToString();
                                ObjMainMenu.MenuIcon = OReader["MenuIcon"].ToString();
                                ObjMainMenu.MenuTitle = OReader["MenuTitle"].ToString();
                                ObjMainMenu.Controllername = OReader["Controllername"].ToString();
                                ObjMainMenu.ActionName = OReader["ActionName"].ToString();
                                ObjMainMenu.Url = OReader["Url"].ToString();
                                ObjMainMenu.OrderNo = (int)OReader["OrderNo"];
                                ObjMainMenu.PageName = OReader["PageName"].ToString();
                                ObjMainMenu.PageDesc = OReader["PageDesc"].ToString();
                                ObjMainMenu.IsActive = (bool)OReader["IsActive"];
                                lstMainMenu.Add(ObjMainMenu);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return lstMainMenu;
        }
        public List<EntityMenuCategory> GetMenuCategory()
        {
            List<EntityMenuCategory> lstMenuCategory = new List<EntityMenuCategory>();
            try
            {
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Menus]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@MenuType", DbType.String, "MENUCATEGORY");
                    SQLServer.AddInParameter(dbcommand, "@Mode", DbType.String, "LIST");
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        using (IDataReader OReader = SQLServer.ExecuteReader(dbcommand))
                        {

                            while (OReader.Read())
                            {
                                EntityMenuCategory ObjMenu = new EntityMenuCategory();
                                ObjMenu.id = (int)OReader["MenuCategoryId"];
                                ObjMenu.MenuCategoryId = (int)OReader["MenuCategoryId"];
                                ObjMenu.MenuCategoryName = OReader["MenuCategoryName"].ToString();
                                ObjMenu.IsActive = (bool)OReader["IsActive"];
                                lstMenuCategory.Add(ObjMenu);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return lstMenuCategory;
        }
        
        
    }
}
